﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Diagnostics; 
using System.Threading;
using System.Collections;
using System.IO;
using System.Drawing.Imaging;

using AForge.Video;
using AForge.Video.DirectShow;

namespace VPC.UI
{
    public partial class OcrForm : Form
    {
        private bool DeviceExist = false;
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource = null;

        public OcrForm()
        {
            InitializeComponent();

            try
            {
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                comboBox1.Items.Clear();
                comboBox1.Visible = false;
                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                DeviceExist = true;
                foreach (FilterInfo device in videoDevices)
                {
                    comboBox1.Items.Add(device.Name);
                }
                comboBox1.SelectedIndex = 0; //make dafault to first cam

                videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
                videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                CloseVideoSource();
                videoSource.DesiredFrameSize = new Size(610, 457);
                videoSource.Start();
            }
            catch (ApplicationException)
            {
                DeviceExist = false;
                comboBox1.Items.Add("No capture device on your system");
            }
        }

        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap img2 = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = img2;
            if (Control.ModifierKeys == Keys.Shift)
            {
                if (videoSource.IsRunning)
                {
                    Bitmap img3 = (Bitmap)eventArgs.Frame.Clone();
                    string doc = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString();
                    doc += "\\test.bmp";
                    if (System.IO.File.Exists(doc))
                    {
                        try
                        {
                            System.IO.File.Delete(doc);
                        }
                        catch (System.IO.IOException e)
                        {
                            Console.WriteLine(e.Message);
                            return;
                        }
                    }
                    img3.Save(doc, ImageFormat.Bmp);
                    //pictureBox1.Image = img3;
                    OcrFormNext frmocrnext = new OcrFormNext();
                    frmocrnext.ShowDialog();

                    //pictureBox1.Image.Save(@"C:\Users\Antoine\My Documents\test.bmp", ImageFormat.Bmp);
                    //videoSource.SignalToStop();

                    //CloseVideoSource();
                    
                    

                    //videoSource = null;

                    

                    //this.Form1_FormClosed_call();

                    

                    ////button1.IsEnabled = false;


                }
                
            }
        }

        private void CloseVideoSource()
        {
            if (!(videoSource == null))
                if (videoSource.IsRunning)
                {
                    videoSource.SignalToStop();
                    videoSource = null;
                }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            CloseVideoSource();
            OcrFormNext frmocrnext = new OcrFormNext();
            this.Hide();
            frmocrnext.ShowDialog();
        }
    }
}
